package com.xh.petdogexpert.sys.service.imp;

import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.sys.dto.Lov;
import com.xh.petdogexpert.sys.dto.LovItem;
import com.xh.petdogexpert.sys.mapper.LovMapper;
import com.xh.petdogexpert.sys.service.ILovItemService;
import com.xh.petdogexpert.sys.service.ILovService;

@Transactional
@Service
public class LovServiceImp implements ILovService {

	@Autowired
	LovMapper lovMapper;
	@Autowired
	ILovItemService lovItemService;
	@Autowired
	private SqlSession sqlSession;

	private Logger logger = LoggerFactory.getLogger(LovServiceImp.class);

	@Override
	public Lov getLov(Lov lov) throws BaseException {
		List<Lov> lovs = lovMapper.queryLovs(lov);
		if (lovs.size() > 1) {
			throw new BaseException("获取多于一个lov的数据");
		} else if (lovs == null || lovs.size() == 0) {
			throw new BaseException("不存在此lov的数据");
		} else {
			return lovs.get(0);
		}

	}

	public List<Lov> getLovs(Lov lov) {
		return lovMapper.queryLovs(lov);
	}

	@Override
	public void addUpdateLov(List<Lov> lovs) throws BaseException {
		try {
			for (Lov lov : lovs) {
				if (lov.getId() != null)
					lovMapper.updateLov(lov);
				else
					lovMapper.addLov(lov);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("保存出错");
		}

	}

	@Override
	public int deleteLov(Lov lov) throws BaseException {
		int count;
		try {
			count = lovMapper.deleteLov(lov);

		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("删除失败");
		}
		return count;
	}

	@Override
	public List<Lov> queryLovs(Lov lov) {
		List<Lov> lovs = lovMapper.queryLovs(lov);
		return lovs;
	}

	/**
	 * 获取lov的方式有3种(有顺序) 1.如果有url就获取url的 2.如果有sqlId就根据sqlId获取
	 * 3.如果上面两个都没有，那么就会获取item里面的list
	 * 
	 * @author xin
	 * @throws BaseException 
	 * @data 2016年3月1日
	 */
	@Override
	public LovEditor getLovList(String lovName) throws BaseException {

		ObjectMapper objectMapper = new ObjectMapper();
		Lov lov = new Lov();
		lov.setLovName(lovName);
		lov = getLov(lov);
		LovEditor lovEditor = new LovEditor(lov);

		List<Object> dataList;

		if (lovEditor.getUrl() != null && !lovEditor.getUrl().trim().equals("")) { //如果有url则直接返回url

		} else if (lov.getSqlid() != null && !lov.getSqlid().trim().equals("")) {//如果有sqlid，则要求出sqlid的内容

			dataList = sqlSession.selectList(lov.getSqlid());
			lovEditor.setData(dataList);
			
		} else {
			LovItem lovItem = new LovItem();
			lovItem.setLovId(lov.getId());
			List<LovItem> lovItems = lovItemService.getLovItems(lovItem);
			
			lovEditor.setTextField("text");
			lovEditor.setValueField("value");
			
			dataList = Arrays.asList(lovItems.toArray());
			lovEditor.setData(dataList);
		}
		
		
		return lovEditor;
	}
	
	
	public String getLovListString(String lovName) throws JsonProcessingException, BaseException {
		LovEditor lovEditor = getLovList(lovName);
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(lovEditor);
		return json.substring(1, json.length() - 1);
	}
	
	
	public class LovEditor {
		String textField;
		String valueField;
		@JsonInclude(Include.NON_NULL)
		String url;
		@JsonInclude(Include.NON_NULL)
		List<Object> data;

		public LovEditor(Lov lov) {
			this.textField = lov.getTextField();
			this.valueField = lov.getValueField();
			this.url = lov.getUrl();
		}

		public String getTextField() {
			return textField;
		}

		public void setTextField(String textField) {
			this.textField = textField;
		}

		public String getValueField() {
			return valueField;
		}

		public void setValueField(String valueField) {
			this.valueField = valueField;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public void setData(List<Object> data) {
			this.data = data;
		}

		public List<Object> getData() {
			return data;
		}
	}
}
